//
//  MTTabBarController.m
//  ShiShiBang
//
//  Created by songbaoqiang on 16/9/1.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTTabBarController.h"
#import "MTNearbyViewController.h"
#import "MTPublishViewController.h"
#import "MTRequirementsViewController.h"
#import "MTSkillManageMent.h"
#import "MTNavigationController.h"
#import "MTMeViewController.h"
#import "MTRequestedViewController.h"

#import "UIImage+Image.h"

#import "JTNavigationController.h"

@interface MTTabBarController ()

@property (nonatomic, strong) MTPublishViewController *publishVC;
@end


@implementation MTTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.translucent = NO;
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tab_middle_bg"];
    self.tabBar.tintColor = [UIColor blackColor];
    
    [self setupAllChildViewController];
    [self setupAllTitleButton];
    [self setUpTabBar];
}

+ (void)load
{
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSForegroundColorAttributeName] = [UIColor greenColor];
    [item setTitleTextAttributes:attr forState:UIControlStateSelected];
    
    NSMutableDictionary *attrNor = [NSMutableDictionary dictionary];
    attrNor[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    [item setTitleTextAttributes:attrNor forState:UIControlStateNormal];
    
}

- (void)setUpTabBar {

    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.image = [UIImage imageNamed:@"tab_middle_bg"];
    [imageV setContentMode:UIViewContentModeCenter];
    imageV.frame = CGRectMake(0, -8, self.tabBar.mt_width, self.tabBar.mt_height);

    [self.tabBar insertSubview:imageV atIndex:0];
    [self.tabBar setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
    [self.tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]];
    
}
//在View显示完毕时, 添加按钮,覆盖在底部按钮之上.
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
   [addBtn setImage:[UIImage imageNamed:@"middle"] forState:UIControlStateNormal];
    CGFloat btnWH = 98;

    addBtn.frame = CGRectMake((MTScreenW - btnWH) * 0.5,-40, btnWH, btnWH);
    [self.tabBar addSubview:addBtn];
   
    [addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupAllChildViewController {
     //附近
    MTNearbyViewController *vc = [[MTNearbyViewController alloc] init];
    JTNavigationController *nav = [[JTNavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:nav];
    //有求
    MTRequirementsViewController *vc1 = [[MTRequirementsViewController alloc] init];
    JTNavigationController *nav1 = [[JTNavigationController alloc] initWithRootViewController:vc1];
    [self addChildViewController:nav1];
    //发布
    [self addChildViewController:[[UIViewController alloc] init] ];
     //必应
    MTRequestedViewController *vc3 = [[MTRequestedViewController alloc] init];
    MTNavigationController *nav3 = [[MTNavigationController alloc] initWithRootViewController:vc3];
    [self addChildViewController:nav3];
    //个人中心
    MTMeViewController *vc4 = [[MTMeViewController alloc] init];
    JTNavigationController *nav4= [[JTNavigationController alloc] initWithRootViewController:vc4];
    [self addChildViewController:nav4];
  
    
}

- (void)setupAllTitleButton {
    
    UIViewController *vc = self.childViewControllers[0];
    vc.tabBarItem.title = @"发现";
    vc.tabBarItem.image = [UIImage imageNamed:@"icon_faxian_gray"];
    vc.tabBarItem.selectedImage = [UIImage imageOriginalWithImageName:@"icon_faxian"];
    
    UIViewController *vc1 = self.childViewControllers[1];
    vc1.tabBarItem.title = @"有求";
    vc1.tabBarItem.image = [UIImage imageNamed:@"icon_youqiu_gray"];
    vc1.tabBarItem.selectedImage = [UIImage imageOriginalWithImageName:@"icon_youqiu"];
    
    UIViewController *vc2 = self.childViewControllers[2];
    vc2.tabBarItem.title = @"发布";

    
    UIViewController *vc3 = self.childViewControllers[3];
    vc3.tabBarItem.title = @"必应";
    vc3.tabBarItem.image = [UIImage imageNamed:@"icon_biying_gray"];
    vc3.tabBarItem.selectedImage = [UIImage imageOriginalWithImageName:@"icon_biying_normal"];
    
    UIViewController *vc4 = self.childViewControllers[4];
    vc4.tabBarItem.title = @"我";
    vc4.tabBarItem.image = [UIImage imageNamed:@"my_gray"];
    vc4.tabBarItem.selectedImage = [UIImage imageOriginalWithImageName:@"my"];
    
}

//添加按钮点击
- (void)addBtnClick {
    
    MTPublishViewController *publishVC = [[MTPublishViewController alloc] init];
    self.publishVC = publishVC;
    __weak typeof(self) weakSelf = self;
    publishVC.closeTask = ^{
        weakSelf.publishVC = nil;
    };
    [[UIApplication sharedApplication].keyWindow addSubview:publishVC.view];
    
}

@end
